/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


public class autoSolve {
	NodeStack s=new NodeStack();
	boolean [][][]path;
	public autoSolve(){
		s=new NodeStack();
		path=null;
	}
  public NodeStack getS() {
		return s;
	}
	public void setS(NodeStack s) {
		this.s = s;
	}
	public boolean[][][] getPath() {
		return path;
	}
	public void setPath(boolean[][][] path) {
		this.path = path;
	}
      public   void makePath(Level[]maze,TermQ answer)
        {
	  boolean [][][]lastPath= new boolean[10][][];
    for(int l=0;l<maze.length;l++){
    	if(maze[l]!=null){
    lastPath[l]=new boolean[maze[l].getLevel().length][maze[l].getLevel().length];
    	}
    }
            while(answer.getParent()!=null){
            	
            	   if(s.isEmpty()||s.top()!=(Integer)answer.level){
    	  s.push(answer.level);
      }
                lastPath[answer.level][answer.positionI][answer.positionJ]=true;
                	
answer=answer.getParent();
            }

            
          this.setPath(lastPath);
        }
      public static void setForQ(Level[]levels) {
          Level[]temps= new Level[10];
  // this.setLevel(levels[levelNo].getLevel());
           TermQ Start = new TermQ(2, 2);
           Start.setLevel(3);
           QueueAlgorithmFllow x = new QueueAlgorithmFllow();
           TermQ answer = x.QueueAlgoPath(levels, Start);
           autoSolve solveQ = new autoSolve();
           solveQ.makePath(levels, answer);
           boolean[][][]solve = solveQ.path;
           int index = 0;
           for (int num = 0; num < solve.length ; num++) {
               if (solve[num] != null) {
                   char[][] currentChar = levels[num].getLevel();
                   boolean[][] currentBoolean = solve[num];
                   for (int i = 0; i < currentChar.length; i++) {
                       for (int j = 0; j < currentChar.length; j++) {
                         
                           if (currentChar[i][j] == ' ' && currentBoolean[i][j] == true) {
                               if (i - 1 >= 0 && currentBoolean[i - 1][j]) {
                                
                                   currentChar[i][j] = 'n';
                               } else if (j - 1 >= 0 && currentBoolean[i][j - 1]) {
                                   currentChar[i ][j] = 'w';
                               } else if (i + 1 < currentBoolean.length && currentBoolean[i + 1][j]) {
                                   currentChar[i][j] = 's';

                               } else if (j + 1 < currentBoolean.length && currentBoolean[i][j + 1]) {
                                   currentChar[i][j] = 'e';

                               } else if (i - 1 >= 0 && j - 1 >= 0 && currentBoolean[i - 1][j - 1]) {
                                   currentChar[i ][j] = 'h';

                               } else if (i - 1 >= 0 && j + 1 < currentBoolean.length && currentBoolean[i - 1][j + 1]) {
                                   currentChar[i][j ] = 'k';

                               } else if (i - 1 >= 0 && j - 1 >= 0 && currentBoolean[i - 1][j - 1]) {
                                   currentChar[i][j] = 'h';

                               } else if (i + 1 < currentBoolean.length && j - 1 >= 0 && currentBoolean[i + 1][j - 1]) {
                                   currentChar[i][j] = 'k';

                               } else if (i + 1 < currentBoolean.length && j + 1 < currentBoolean.length && currentBoolean[i + 1][j + 1]) {
                                   currentChar[i ][j ] = 'l';

                               }

                           }
                       }
                   }
                

               }
            
           }
          
           for(index=0;index<levels.length&&!solveQ.s.isEmpty();index++){
                   int newPlace = (Integer) solveQ.s.pop();
                   System.out.println(newPlace);
   temps[index]=levels[newPlace];  
           }




           levels=temps;
       //     levelNo=0;


       }
  public static void main (String[]args){
        GenerateLevel lev = new GenerateLevel("C:\\My Documents\\map.txt");
            Level[]array = lev.getLevels();
            TermQ newTerm = new TermQ(2,2);
            newTerm.setLevel(3);
            newTerm.setParent(null);
            setForQ(array);
            QueueAlgorithmFllow x = new QueueAlgorithmFllow();
            TermQ answer = x.QueueAlgoPath(array,newTerm);
            autoSolve solve = new autoSolve();
           solve.makePath(array, answer);
           boolean[][][]ans=solve.path;
           int l=0;
           while(!solve.s.isEmpty()){
     //   int rand=(int) (5 * Math.random());
        	   l=(Integer) solve.s.pop();
		if(ans[l]!=null){
       boolean[][]currLevel=ans[l];

        for(int i=0;i<currLevel.length;i++)
        {
            for(int j=0;j<currLevel.length;j++)
            {
       System.out.print(currLevel[i][j]+" ");
            }
            System.out.println("");
        }
        //hola
        System.out.println("----------------------------");
        
	}
		
           }
        
      }
      

  }
